<?php
require_once('application/libraries/user.php');
class UserDAL extends CI_Model{
	private $userTableName = "users";
	function usernameExists($user){
		$username = $user->getUsername();
		// Returns true if username exists
		$sql = "SELECT * FROM $this->userTableName WHERE USERNAME = ?";
		$query = $this->db->query($sql, array($username));
		if($query->num_rows == 1){
			return TRUE;
		}
		else {
		return FALSE;
		}
	}
	function addUser($firstname, $lastname, $email, $username, $password){
		$sql = "INSERT INTO $this->userTableName (firstname, lastname, email, username, password) VALUES(?, ?, ?, ?, ?)";
		$query = $this->db->query($sql, array($firstname, $lastname, $email, $username, $password));
		return $query;
	}
	public function deleteUser($username) {
		$sql = "DELETE FROM $this->userTableName WHERE Username = ?";
		$query = $this->db->query($sql, array($username));
		return $query;
	}
	public function validLogin($username, $password){
		// Returns true if login input is valid
		$sql = "SELECT * FROM $this->userTableName WHERE (username = ? AND password = ?)";
		$query = $this->db->query($sql, array($username, $password));
		if($query->num_rows == 1){
			return TRUE;
		}
		else {
		return FALSE;
		}
	}
	public function getPassword($username){
		// Returns password corresponding to input username
		$sql = "SELECT password FROM $this->userTableName WHERE USERNAME = ?";
		$query = $this->db->query($sql, array($username));
		if ($query->num_rows() > 0){
 		  $row = $query->row(); 
 		  return $row->password;
		}
	}
}